﻿@model List<DHQGHN.PM2.Models.EnumeratorModel>
<link href="~/Content/index.css" rel="stylesheet" />

@{
    //ViewBag.Title = "Quản lý Admin Xã/Phường";
    ViewBag.Group = "Quản lý chung";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
    DHQGHN.PM2.Security.MyPrincipalSerializeModel myUser = Session["UserInfo"] as DHQGHN.PM2.Security.MyPrincipalSerializeModel;
    ViewBag.ADMID =  myUser != null ? myUser.AdminCode : "";
    string ActionCreate = "";
    string ActionEdit = "";
    string GroupName = "";
    string Level = "";
    List<SelectListItem> listTinh = null;
    List<SelectListItem> listHuyen = null;
    List<SelectListItem> listXa = null;
    
    if (myUser.GroupCode.Equals("AM"))
    {
        ViewBag.Title = "Quản lý Admin Tỉnh/TP";
        ActionCreate = "CreateAdminTinh";
        ActionEdit = "EditAdminTinh";
        GroupName = "Quản Trị Tỉnh/TP";
        Level = "Level1t";
        if (ViewBag.Level1 != null)
        {
            listTinh = ViewBag.ListTinhSelected as List<SelectListItem>;
        }
        else { listTinh = ViewBag.ListTinh as List<SelectListItem>; }
        listHuyen = ViewBag.ListHuyen as List<SelectListItem>;
        listXa = ViewBag.ListXa as List<SelectListItem>;
        <script>
            $(function () {
                $(document).ready(function () {
                    $('#AdminCode').val($('#Level1').val());
                    $('#Level2').empty();
                    $('#Level3').empty();
                    $('#Level2').attr("disabled", "disabled");
                    $('#Level3').attr("disabled", "disabled");
                    $('#Level3').hide(); $('#lb3').hide();
                    $('#Level2').hide(); $('#lb2').hide();
                });
                $('#Level1').change(function () {
                    $('#divLoading').show();
                    var parentCode = $('#Level1').val();
                    if (parentCode != "-1") {
                        $('#divLoading').hide();
                        $('#AdminCode').val($('#Level1').val());
                    } else {
                        $('#divLoading').hide();
                        $('#AdminCode').val(null);
                    }
                    return false;
                });
            });
        </script>
    }
    else if (myUser.GroupCode.Equals("AT"))
    {
        ViewBag.Title = "Quản lý Admin Quận/Huyện";
        ActionCreate = "CreateAdminHuyen";
        ActionEdit = "EditAdminHuyen";
        GroupName = "Quản Trị Quận/Huyện";
        Level = "Level2h";
        listTinh = ViewBag.ListTinhSelected as List<SelectListItem>;
        if (ViewBag.Level2 != null)
        {
            listHuyen = ViewBag.ListHuyenSelected as List<SelectListItem>;
        }
        else
        { listHuyen = ViewBag.ListHuyen as List<SelectListItem>; }
        listXa = ViewBag.ListXa as List<SelectListItem>;
        <script>
            $(function () {
                $(document).ready(function () {
                    $('#AdminCode').val($('#Level2').val());
                    $('#Level3').empty();
                    $('#Level1').attr("disabled", "disabled");
                    $('#Level3').attr("disabled", "disabled");
                    $('#Level3').hide(); $('#lb3').hide();
                    return false;
                });
                $('#Level2').change(function () {
                    $('#divLoading').show();
                    var parentCode = $('#Level2').val();
                    if (parentCode != "-1") {
                        $('#divLoading').hide();
                        $('#AdminCode').val($('#Level2').val());
                    } else {
                        $('#divLoading').hide();
                        $('#AdminCode').val(null);
                    }
                    return false;
                });
            });
        </script>
        
    }
    else if (myUser.GroupCode.Equals("AH"))
    {
        ViewBag.Title = "Quản lý Admin Xã/Phường";
        ActionCreate = "CreateAdmin";
        ActionEdit = "  ";
        GroupName = "Quản Trị Xã/Phường";
        Level = "Level3x";
        listTinh = ViewBag.ListTinhSelected as List<SelectListItem>;
        listHuyen = ViewBag.ListHuyenSelected as List<SelectListItem>;
        if (ViewBag.Level3 != null)
        {
            listXa = ViewBag.ListXaSelected as List<SelectListItem>;
        }
        else
        { listXa = ViewBag.ListXa as List<SelectListItem>; }
        <script>
            $(function () {
                $(document).ready(function () {
                    $('#AdminCode').val($('#Level3').val());
                    $('#Level1').attr("disabled", "disabled");
                    $('#Level2').attr("disabled", "disabled");
                    return false;
                });
                $('#Level3').change(function () {
                    $('#divLoading').show();
                    var parentCode = $('#Level3').val();
                    if (parentCode != "-1") {
                        $('#divLoading').hide();
                        $('#AdminCode').val($('#Level3').val());
                    } else {
                        $('#divLoading').hide();
                        $('#AdminCode').val(null);
                    }
                    return false;
                });
            });
        </script>
        
    }
}
@{
    var grid = new WebGrid(canPage: true, rowsPerPage: 10, selectionFieldName: "selectedRow");
    if (Model.Count > 0)
    {
        grid.Bind(@Model, autoSortAndPage: false, rowCount: Model[0].RECORD_COUNT);
    }
    else
    {
        grid.Bind(@Model, autoSortAndPage: false, rowCount: 0);
    }
    grid.Pager(WebGridPagerModes.NextPrevious);
}

<!--end_bootstrap -->
<link href="~/Content/standard_form_nhaplieu.css" rel="stylesheet" />
<script>
    $(function () {
        @functions{
                    public string GetAntiForgeryToken()
                    {
                        string cookieToken, formToken;
                        AntiForgery.GetTokens(null, out cookieToken, out formToken);
                        return cookieToken + "," + formToken;
                    }
                }
    });
    function onDelete(id) {        
        $("#divLoading").show();
        $.ajax({
            url: '@Url.Action("ViewForDelete")',
            data: {
                "ENUMERATOR_ID": id,
                "Admin": "admin"
            },
            type: 'POST',
            success: function (data) {
                $("#divLoading").hide();
                bootbox.dialog({
                    message: data,
                    buttons: {
                        success: {
                            label: "<i class='fa fa-warning'></i> Xóa",
                            className: "btn btn-sm btn-danger", //btn-success
                            callback: function () {                                
                                $("#divLoading").show();
                                $.ajax({
                                    url: "@Url.Action("Delete")",
                                    data: { "id": id },
                                    type: 'post',
                                    headers: {
                                        'VerificationToken': '@GetAntiForgeryToken()'
                                    },
                                    success: function (d) {
                                        $("#divLoading").hide();
                                        Layout.toastr("", "Xóa thành công");
                                        setTimeout(function () {
                                            window.location.reload(true);
                                        }, 2000);
                                    },
                                    error: function (ex) {
                                        alert(JSON.stringify(ex));
                                    }
                                });

                            }
                        },
                        danger: {
                            label: "<i class='fa fa-reply'></i> Hủy bỏ",
                            className: "btn btn-sm btn-default",//"btn-danger",
                            callback: function () {
                            }
                        }
                    }
                });
            }
        });
        return false;
    };

    function onEdit(id) {        
        $("#divLoading").show();
        $.ajax({
            url: '@Url.Action(ActionEdit)',
            data: {
                "EnumID": id
            },
            type: 'GET',
            headers: {
                'VerificationToken': '@GetAntiForgeryToken()'
            },
            success: function (data) {
                $("#divLoading").hide();
                bootbox.dialog({
                    message: data,
                    buttons: {
                        success: {
                            label: "<i class='fa fa-edit'></i> Sửa",
                            className: "btn btn-sm btn-success", //btn-success
                            callback: function () {
                                var myForm = $('#' + '@ActionEdit');
                                if (!myForm.valid()) {
                                    return false;
                                }
                                //alert("email: " + $("#EMAIL").val());
                                var ajax = $.ajax({
                                    url: '@Url.Action(@ActionEdit)',
                                    type: 'POST',
                                    headers: {
                                        'VerificationToken': '@GetAntiForgeryToken()'
                                    },
                                    data: {
                                        "ENUMERATOR_ID" : $("#ENUMERATOR_ID").val(),
                                        "GROUP_CODE": $("#GroupCode").val(),
                                        "ADM_CODE": $("#" + "@Level").val(),
                                        "FULL_NAME": $("#FULL_NAME").val(),
                                        "USERNAME": $("#USERNAME").val(),
                                        "EMAIL": $("#EMAIL").val(),
                                        "DESCRIPTIONS": $("#DESCRIPTIONS").val()
                                    },
                                    success: function (result) {
                                        Layout.toastr("",result);
                                        setTimeout(function () {
                                            window.location.reload(true);
                                        }, 2000);
                                    },
                                    complete: function () {
                                    },
                                    error: function (ex) {
                                        alert(JSON.stringify(ex));
                                    }
                                });
                            }
                        },
                        danger: {
                            label: "<i class='fa fa-reply'></i> Hủy bỏ",
                            className: "btn btn-sm btn-default",//"btn-danger",
                            callback: function () {
                            }
                        }
                    }
                });
            }
        });
        return false;
    }

    function onCreate() {        
        $("#divLoading").show();
        $.ajax({            
            url: '@Url.Action(ActionCreate)',            
            type: 'GET',
            success: function (data) {
                $("#divLoading").hide();
                bootbox.dialog({
                    message: data,
                    buttons: {
                        success: {
                            label: "<i class='fa fa-plus-square-o'></i> Tạo mới",
                            className: "btn btn-sm btn-success", //btn-success
                            callback: function () {
                                var myForm = $('#' + '@ActionCreate');
                                if (!myForm.valid())  return false;
                                //$('#' + '@ActionCreate').submit();                               
                                var ajax = $.ajax({
                                     url: '@Url.Action(@ActionCreate)',                                    
                                    type: 'POST',
                                    headers: {
                                        'VerificationToken': '@GetAntiForgeryToken()'
                                    },
                                    data:{
                                        "GROUP_CODE": $("#GroupCode").val(),
                                        "ADM_CODE": $("#" + "@Level").val(),
                                        "FULL_NAME": $("#FULL_NAME").val(),
                                        "USERNAME": $("#USERNAME").val(),
                                        "EMAIL": $("#EMAIL").val(),
                                        "DESCRIPTIONS": $("#DESCRIPTIONS").val()
                                    },
                                    success: function (result) {
                                        console.log(result);
                                        Layout.toastr("", result);
                                        setTimeout(function () {
                                            window.location.reload(true);
                                        }, 2000);
                                    },
                                    complete: function(){

                                    },
                                    error: function(ex){
                                        alert(JSON.stringify(ex));
                                    }
                                });                                
                            }
                        },
                        danger: {
                            label: "<i class='fa fa-reply'></i> Hủy bỏ",
                            className: "btn btn-sm btn-default",//"btn-danger",
                            callback: function () {
                            }
                        }
                    }
                });
            },
            error: function (ex)
            { alert(JSON.stringify(ex));}
        });
        return false;
    }
</script>
<div class="page-bar">
    <ul class="page-breadcrumb" style="padding: 4px;">
        <li>
            <i class="fa fa-home"></i>
            <a href="#">@ViewBag.Title</a>
        </li>
    </ul>
    @*<div class="page-toolbar">
            <button type="button" class="btn input-sm " style="width:90px;" onclick="window.location.href='@Url.Action(ActionCreate)';"><i class="fa  fa-plus-square-o"></i> Tạo mới</button>
        </div>*@
</div>

@using (Html.BeginForm("SearchAdminPost", "Enumerator", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    <div class="row margin-bottom-20">
        <div class="col-md-5">
            <div class="row" style="margin-top:5px;">
                @Html.Label("Cấp Tỉnh", new { @class = "col-md-3", id = "lb1" })
                <div class="col-md-9">
                    @*@Html.DropDownList("Level1", new SelectList(string.Empty, "Value", "Text"), new { id = "Level1", style = "margin:0px;", @class = "form-control input-sm" })*@
                    @Html.DropDownList("Level1", listTinh, new { id = "Level1", style = "margin:0px;", @class = "form-control input-sm" })
                </div>
            </div>
            <div class="row" style="margin-top:0px;">
                @Html.Label("Cấp Huyện", new { @class = "col-md-3", style = "white-space: nowrap;", id = "lb2" })
                <div class="col-md-9">
                    @*@Html.DropDownList("Level2", new SelectList(string.Empty, "Value", "Text"), new { id = "Level2", style = "margin:0px;", @class = "form-control input-sm" })*@
                    @Html.DropDownList("Level2", listHuyen, new { id = "Level2", style = "margin:0px;", @class = "form-control input-sm" })
                </div>
            </div>
            <div class="row" style="margin-top:0px;">
                @Html.Label("Cấp Xã", new { @class = "col-md-3", id = "lb3" })
                <div class="col-md-9">
                    @Html.DropDownList("Level3", listXa, new { id = "Level3", style = "margin:0px;", @class = "form-control input-sm" })
                    @Html.Hidden("AdminCode", new { id = "AdminCode", name = "AdminCode" })
                </div>
            </div>
            <div class="row" style="margin-top:5px;">
                @Html.Label("Nhóm", new { @class = "col-md-3" })
                <div class="col-md-9">
                    @Html.TextBox("GROUP", @GroupName /*"Quản trị cấp Xã/Phường"*/, new { @class = "form-control input-sm", style = "", @readOnly = true })
                </div>
            </div>
        </div>
        <div class="col-md-5">

            <div class="row" style="margin-top:5px;">
                @Html.Label("Họ tên", new { @class = "col-md-3" })
                <div class="col-md-9">
                    @Html.TextBox("FULL_NAME", "", new {@id= "FULL_NAME_SEARCH", @class = "form-control input-sm", style = "", placeholder = "nhập họ tên" })
                </div>
            </div>
            <div class="row" style="margin-top:5px;">
                @Html.Label("Tên đăng nhập", new { @class = "col-md-3", style = "white-space: nowrap;" })
                <div class="col-md-9">
                    @Html.TextBox("USERNAME", "", new {@id="USERNAME_SEARCH", @class = "form-control input-sm", style = "", placeholder = "tên đăng nhập" })
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="row" style="margin-top:5px; text-align:right;">
                <div class="col-md-12">
                    <button type="button" class="btn btn-sm btn-default" style="width: 90px; margin-bottom: 5px; cursor:pointer;" onclick="onCreate()"><i class="fa fa-plus-square-o"></i> Tạo mới</button>
                    <button type="submit" class="btn btn-sm btn-default" id="btnSearch" style="width:90px;" name="command" value="Search"><i class="fa fa-search"></i> Tìm kiếm</button>
                </div>
            </div>
        </div>
    </div>
}
<div id="gridContent" align="center">
    @if (Model.Count > 0)
    {
        if (Model.First().RECORD_COUNT <= 10)
            {
                <script>
                    $(document).ready(function () {
                        $('.grid-footer').empty();
                        $('#EnumGrid').append('<tfoot><tr class = "grid-footer"><td colspan="7"></td></tr></tfoot>')
                    })
                </script>
            }
        <script>
            $(document).ready(function () {
                $('.grid-footer td').append('<div style="float:right; ">Đang xem ' + '@Model.First().ORDER' + ' - ' + '@Model.Last().ORDER' + ' trên ' + '@Model.First().RECORD_COUNT' + ' bản ghi</div>')
            })
        </script>
        @grid.GetHtml(tableStyle: "table table-striped table-bordered table-hover dataTable no-footer",
                  htmlAttributes: new { id = "EnumGrid" },
                  headerStyle: "header",
                    alternatingRowStyle: "",
                    selectedRowStyle: "select",
                    rowStyle: "webgrid-row-style",
                    footerStyle: "grid-footer",
                    mode: WebGridPagerModes.All,
                    firstText: "|<<",
                    previousText: "|<",
                    nextText: ">|",
                    lastText: ">>|",
                    columns: grid.Columns(
                    grid.Column(
                        columnName: "STT",
                        header: "STT",
                        format: @<text>
                            @item.ORDER
                        </text>,
                        canSort: false,
                        style: "clsSTT"
                    ),
                          grid.Column(
                            columnName: "FullName",
                            header: "Họ và tên",
                            format: @<text>
                                <div id="spnFullName" class="display-mode">
                                    <span class="hidden-780">@item.FULL_NAME</span>
                                    <span class="display-780"><b>@item.FULL_NAME</b></span>
                                </div>
                                <div class="display-480">Tên đăng nhập: @item.USERNAME</div>
                                <div class="display-580">Nhóm: @item.GROUPCODE</div>
                                <div class="display-680">Khu hành chính: @item.PROVINCE_NAME - @item.DISTRICT_NAME - @item.COMMUNE_NAME</div>
                                <div class="display-780">Mô tả: @item.DESCRIPTIONS</div>
                            </text>, canSort: false
                          ),
                          grid.Column(
                            columnName:"UserName",
                            header: "Tên đăng nhập",
                            style: "clsUserName hidden-480",
                            format: @<text>
                                <span id="spnUserName" class="display-mode">
                                    @item.USERNAME
                                </span>
                            </text>, canSort: false
                          ),
                          grid.Column(
                            columnName:"GroupCode",
                            header: "Nhóm",
                            style: "clsGroupCode hidden-580",
                            format: @<text>
                                <span id="spnGroupCode" class="display-mode">
                                    @item.GROUPCODE
                                </span>
                            </text>, canSort: false
                          ),
                          grid.Column(
                            columnName:"ADM_UNIT",
                            header: "Khu hành chính",
                            style: "clsADM_UNIT hidden-680",
                            format: @<text>
                                @if (item.DISTRICT_NAME != null && item.COMMUNE_NAME != null)
                                {
                                    <span id="spnFullName" class="display-mode">
                                        @item.PROVINCE_NAME - @item.DISTRICT_NAME - @item.COMMUNE_NAME
                                    </span>
                                }
                                else if (item.DISTRICT_NAME != null && item.COMMUNE_NAME == null)
                                {
                                    <span id="spnFullName" class="display-mode">
                                        @item.PROVINCE_NAME - @item.DISTRICT_NAME
                                    </span>
                                }
                                else if (item.DISTRICT_NAME == null)
                                {
                                    <span id="spnFullName" class="display-mode">
                                        @item.PROVINCE_NAME
                                    </span>
                                }
                            </text>, canSort: false
                          ),
                           grid.Column(
                            columnName:"Descriptions",
                            header: "Mô tả",
                            style: "clsDescriptions hidden-780 autohidden",
                            format: @<text>
                                <span id="spnGroupCode" class="display-mode">
                                    @item.DESCRIPTIONS
                                </span>
                            </text>, canSort: false
                          ),
                          grid.Column("Thao tác",format: 
                          @<text>
                                <div style="text-align:center;">
                                    <div class='btn-group'>
                                        <a href='#' data-toggle='dropdown' class='btn btn-lg' style="margin:0px; padding:0px;">
                                            <div class='label label-info'>
                                                <i class='fa fa-th'></i>
                                            </div>
                                        </a>
                                        <ul class='dropdown-menu no-angle pull-right right'>
                                            <li>
                                                @*<a href="@Url.Action(ActionEdit,"Enumerator", new { EnumID = item.ENUMERATOR_ID})"><i class='fa fa-pencil'></i> Cập nhập thông tin</a>*@
                                                <a onclick="onEdit('@item.ENUMERATOR_ID')" data-index="@item.ENUMERATOR_ID" style="cursor:pointer;"><i class='fa fa-pencil'></i> Cập nhập thông tin</a>
                                            </li>
                                            <li>
                                                <a onclick="onDelete('@item.ENUMERATOR_ID')" data-index="@item.ENUMERATOR_ID" style="cursor:pointer;"> <i class="fa fa-trash-o"></i> Xóa thông tin</a>
                                            </li>
                                        </ul>
                                    </div>
                                </div>
                            </text>, canSort: false, style: "clsAction"
                                                              )
                                                    )
                                              )

        <script type="text/javascript">
            $(function () {
                $('.clsSTT').css('text-align', 'center').css('width', '30px');
                var colsHeader = $('#gridContent th');
                $(colsHeader[2]).addClass('hidden-480');
                $(colsHeader[3]).addClass('hidden-580');
                $(colsHeader[4]).addClass('hidden-680');
                $(colsHeader[5]).addClass('hidden-780');
            });
        </script>
    }
    else
    {
        <div align="center"><h4>Không tìm thấy kết quả phù hợp</h4></div>
    }

</div>